package com.wangzhenyu.common.view

import android.content.Context
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.TextView
import com.wangzhenyu.common.R

//错误页面
class EmptyLayout : RelativeLayout {

    companion object {

        const val NO_NETWORK = 1 //没有网络

        const val LOADING = 2 //加载

        const val NO_DATA = 3 //没有数据

        const val HIDE_LAYOUT = 4 //全部隐藏

        const val NODATA_ENABLE_CLICK = 5 //没有数据,可以点击

        const val DATA_ERROR = 6 //数据错误
    }

    private var context: Context? = null
    private var errorState = 0


    private var pwLoading: ProgressBar? = null
    private var tvError: TextView? = null
    private var ivError: ImageView? = null


    constructor(context: Context?) : super(context) {
        this.context = context
        init()
    }

    constructor  (context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        this.context = context
        init()
    }

    private fun init() {
        val view = inflate(context, R.layout.view_error_layout, this)
        setBackgroundColor(-1)
        //ProgressBar
        pwLoading = view.findViewById(R.id.pw_loading)
        //ImageView
        ivError = view.findViewById(R.id.iv_error_layout)
        //TextView
        tvError = view.findViewById(R.id.tv_error_layout)
    }

    //没有网络
    fun isLoadError() = errorState == NO_NETWORK


    //加载
    fun isLoading() = errorState == LOADING

    //全部隐藏
    fun dismiss() {
        errorState = HIDE_LAYOUT
        visibility = GONE
    }

    fun getErrorState(): Int {
        return errorState
    }

    //显示错误原因
    fun setErrorText(text: String?) {
        visibility = VISIBLE
        pwLoading?.visibility = GONE
        tvError?.text = text
    }

    //显示错误类型
    fun setErrorType(errorType: Int) {
        errorState = errorType
        //全部隐藏
        if (errorState == HIDE_LAYOUT) {
            visibility = GONE
            return
        }

        visibility = VISIBLE
        pwLoading?.visibility = GONE
        when (errorType) {
            NO_NETWORK ->
                tvError?.setText(R.string.error_view_network_error_click_to_refresh)

            LOADING -> {
                pwLoading?.visibility = VISIBLE
//                pwLoading.startSpinning();
//                tvError.setVisibility(View.VISIBLE);
                tvError?.setText(R.string.loading)
            }

            NO_DATA -> //                ivError.setImageResource(R.mipmap.ic_no_content);
                tvError?.setText(R.string.error_view_no_data)

            NODATA_ENABLE_CLICK -> //                ivError.setImageResource(R.mipmap.ic_no_content);
                tvError?.setText(R.string.error_view_no_data)

            DATA_ERROR -> //                ivError.setImageResource(R.mipmap.ic_data_error);
                tvError?.setText(R.string.error_view_data_error)

            else -> {}
        }
    }

    //设置整个布局的可见性
    override fun setVisibility(visibility: Int) {
        if (visibility == GONE) {
            errorState = HIDE_LAYOUT
        }
        super.setVisibility(visibility)
    }


    fun setNoDataImage(resId: Int) {
        ivError!!.setImageResource(resId)
    }


}